package com.myapp.actions.visit;

import com.myapp.entity.Client;
import com.myapp.entity.Visit;
import com.myweb.WebAction;
import com.myweb.WebException;
import com.myweb.WebRequest;
import com.myweb.WebResponse;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

public class UpdateVisit implements WebAction {

    private Visit visit = new Visit();

    @Override
    public void prepare(WebRequest request) throws WebException {
        visit.setId(request.getLongNN("id"));
        Client client = new Client();
        client.setId(request.getLongNN("clientId"));
        visit.setClient(client);
        visit.setReasonForConsult(request.getStringNN("reasonForConsult"));
        visit.setHistoryOfPresentIllness(request.getString("historyOfPresentIllness"));
        visit.setSubjective(request.getString("subjective"));
        visit.setObjective(request.getString("objective"));
        visit.setAssessment(request.getString("assessment"));
        visit.setPlan(request.getString("plan"));
    }

    @Override
    public EntityManager process(WebRequest req, WebResponse res, EntityManager em) throws Exception {
        Visit ref = em.getReference(Visit.class, visit.getId());
        Client cref = em.getReference(Client.class, visit.getClient().getId());
        ref.setClient(cref);
        ref.setReasonForConsult(visit.getReasonForConsult());
        ref.setHistoryOfPresentIllness(visit.getHistoryOfPresentIllness());
        ref.setSubjective(visit.getSubjective());
        ref.setObjective(visit.getObjective());
        ref.setAssessment(visit.getAssessment());
        ref.setPlan(visit.getPlan());
        res.setMessage("Successfully updated visit");
        res.setResult(ref);
        return em;
    }
}
